import { _imageRealPath_1 } from "@/utils/imageUtil";
import { defineStore } from "pinia";


interface PassModuleType {
  icon: string ;
  moduleKey: string,
  menuName: string;
}

export interface StateModuleItemType {
  module_key:string,
  module_name: string;  //模块名称
  module_icon_real_path: string, //icon 路径
}
interface StateMenuItemType {
  menuName: string | null;
  menuType: string,
  path: string;
  children: StateMenuItemType[];
}
interface StateStoreType {
  auth_module_cache: Array<StateModuleItemType>;
  auth_module_menu_cache: Record<string, Array<StateMenuItemType>>;
  auth_routes_cache: Array<string>;
  active_module : StateModuleItemType;
}

//用户数据存储
export const moduleStore = defineStore('moduleStore', {
  state: (): StateStoreType => ({
    auth_module_cache: [],
    auth_module_menu_cache: {},
    auth_routes_cache: [],
    active_module: {module_key:'' , module_name: '', module_icon_real_path: ''},
  }),
  persist: [
    {
      storage: localStorage,
      paths: ['auth_module_cache'],
      key: 'AUTH_MODULE_CACHE'
    },
    {
      storage: localStorage,
      paths: ['auth_module_menu_cache'],
      key: 'AUTH_MODULE_MENU_CACHE'
    },
    {
      storage: localStorage,
      paths: ['auth_routes_cache'],
      key: 'AUTH_ROUTES_CACHE'
    },
    {
      storage: localStorage,
      paths: ['active_module'],
      key: 'ACTIVE_MODULE'
    },
  ],
  getters: {
  },
  actions: {
    //获取用户授权的模块数据
    getModules() {
      return this.auth_module_cache;
    },
    getActiveModule() {
      return this.active_module;
    },
    setActiveModule(module : StateModuleItemType) {
      this.active_module = module;
    },
    //获取模块的菜单数据
    getModulesMenus(key: string) {
      console.log(this.auth_module_menu_cache)
      return this.auth_module_menu_cache[key];
    },
    //校验菜单是否有权限
    hasPermission(route :string){
      return this.auth_routes_cache.indexOf(route) != -1;
    },
    //清空所有模块资源
    clearModuleMap() {
      this.auth_module_cache = [];
      this.auth_module_menu_cache = {};
      this.auth_routes_cache = [];
      this.active_module = {module_key:'' , module_name: '', module_icon_real_path: ''};
    },
    //重新加载模块数据.
    async reloadModuleInfos(moduleinfo : PassModuleType[], menuTreeMap :Record<string, Array<StateMenuItemType>>) {
      this.clearModuleMap();
      const authModulesArr = authModules(moduleinfo);
      this.auth_module_menu_cache = menuTreeMap;
      if (authModulesArr) {
        this.auth_module_cache = authModulesArr;
      }
      console.log(this.auth_module_menu_cache)
      return;
    },
  }
})

//后端授权的模块
const authModules = (moduleinfo : PassModuleType[]) => {
  const arr: StateModuleItemType[] = []; // Initialize arr as an array of StateModuleItemType
  moduleinfo.forEach(item => { // Use forEach instead of map since we are not returning a new array
    arr.push({
      module_name: item.menuName,
      module_icon_real_path: _imageRealPath_1(import.meta.url, item.icon, '.png'),
      module_key: item.moduleKey
    });
  });
  console.error(arr);
  return arr;
}
